package com.ramblingcoder.bc2.server.tool.update.objects;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.NoSuchAlgorithmException;

import com.ramblingcoder.bc2.server.tool.update.BC2ServerChoice;
import com.ramblingcoder.bc2.server.tool.update.commands.Login;
import com.ramblingcoder.bc2.server.tool.update.exceptions.InvalidArguments;
import com.ramblingcoder.bc2.server.tool.update.exceptions.LogInRequired;

public class InfoShare {
	//Variables-----\
	private static Socket command = null;
	private static Socket events = null;
	private static Server server = null;
	//Variables-----/
	
	//Processes-----\
	public static boolean initializeServer(Server server) throws UnknownHostException, NoSuchAlgorithmException, IOException, InvalidArguments, LogInRequired{
		initializeSockets(server);
		InfoShare.server = server;
		
		boolean pass1, pass2;
		if (BC2ServerChoice.isLoginEncrypted()){
			pass1 = Login.hashed(command, server.getPass());
			pass2 = Login.hashed(events, server.getPass());
		} else {
			pass1 = Login.plainText(command, server.getPass());
			pass2 = Login.plainText(events, server.getPass());
		}
		
		if (pass1 && pass2){
			Server.enableEvents(events, Server.YES);
			
			return true;
		}
		
		return false;
	}
	
	public static void initializeSockets(Server server) throws UnknownHostException, IOException, NoSuchAlgorithmException {
		command = new Socket();
		command.setSoTimeout(5000);
		command.connect(new InetSocketAddress(server.getIp(), server.getPort()), 5000);
		
		events = new Socket();
		events.connect(new InetSocketAddress(server.getIp(), server.getPort()), 5000);
	}
	
	public static boolean reinitializeSockets() throws UnknownHostException, NoSuchAlgorithmException, IOException, InvalidArguments, LogInRequired{
		return initializeServer(server);
	}
	//Processes-----/
	
	//Getters/Setters-----\
	public static Socket getCommand() {
		return command;
	}
	public static Socket getEvents() {
		return events;
	}
	
	public static void setCommand(Socket command) {
		InfoShare.command = command;
	}
	public static void setEvents(Socket events) {
		InfoShare.events = events;
	}
	//Getters/Setters-----/
}
